/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.communication.demo;

import org.gokeep.common.component.TokenComponent;
import org.gokeep.common.constant.enums.ClientTypeEnum;
import org.gokeep.common.constant.enums.GroupMemberStatusEnum;
import org.gokeep.common.constant.enums.GroupStatusEnum;
import org.gokeep.common.document.Group;
import org.gokeep.common.document.GroupMember;
import org.gokeep.common.document.User;
import org.gokeep.common.dto.TokenPayload;

import java.util.*;

public class TestData {
    public static final String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANPo71t2dd/k7Cy3J5Dnkpp6J//eZcRiN0a/kau49eAgUf9ywrhLfmRmzL4f4ifzzDVz0+gX6q6ZpHOK6pd5OXAUhiVy/Ovay3aeb1Z/nsCrhWlb0qbGYSKptpdOgshL1Bq5klLhmTJGQVdyyVQiS5Z+Xme09Qy5p5L0Euv3Gy1lAgMBAAECgYBNEG2XpA17BnwTSOpVKroofps0VJNK53L1wi4+No4IW4imbvw8XIwTBEWaMOskn2dY2w4p3iTFKW/iaxckTfwiSpQCjjHXXCx9bk7fnSn9e0mnx1k88QBk5hv/1SXD8DXdBuasFtkBtV4v9a7WtVkUha4ORS3hdbNv5jgZrFw8swJBAOlU73I9mhB9bhvqe+ODGURUTBLd/P50UXSgDta7x1VVAPmdbBskgdmtnRxYREKJdAtmD80OAN4p8+TBAofD088CQQDofzpk1FWDDKaJSxrUt+NjvhH61xxwxdrJ2WD3MY+NZskNyP3aOVPQoz7QwPCi/gt/bdS7ZRI1vT7jEPjI1RSLAkEAwnI0pThu5qUQmJT9L2k2Kjoxt1tllUusAgvMseiEwjj3eg1VZP00dQn4rwFjySsxGLqyGkk8Meg2RwENscRhAwJAJgl6ojhi7RfKvzd8YkTbfNwB0QoUaxbTnqZjyM1g8P9Z8VlqvqMWZVHv2vNV6zSo6OYtIaN9gTE+8qM6MJE71QJAcY6X7THZvZnFEL2nzu+SuCqyYW4xxeBdII2ZyWgpWpgcdQGXlCOi/cfoF9OS8SyuVijmtp9XZq894dNIayMKzw==";
    public static final String INIT_COMPANY_ID = "641022ed15a6357eb847e71a";
    public static final String INIT_GROUP_ID = "64101b8e3a403d346ec8a005";


    /**
     * 生成测试数据，仅作为临时测试使用
     *
     * @return
     */
    public static Map<String, User> generateTestUser() {
        List<User> users = getUserList();
        Map<String, User> map = new HashMap<>();
        users.forEach(user -> {
            map.put(user.getUsername(), user);
        });
        return map;
    }

    /**
     * 生成测试数据
     * @return
     */
    public static List<User> getUserList() {
        User user1 = new User().setUserId("64101b8e3a403d346ec8a000").setUsername("xuning").setNickname("徐宁").setPassword("123456");
        User user2 = new User().setUserId("64101b8e3a403d346ec8a001").setUsername("shenmiao").setNickname("申苗").setPassword("123456");
        User user3 = new User().setUserId("64101b8e3a403d346ec8a002").setUsername("zhangsan").setNickname("张三").setPassword("123456");
        User user4 = new User().setUserId("64101b8e3a403d346ec8a003").setUsername("lisi").setNickname("李四").setPassword("123456");
        return Arrays.asList(user1, user2, user3, user4);
    }


    /**
     * 获取测试组
     *
     * @return
     */
    public static Group getGroup() {
        Group group = new Group().
                setGroupId(INIT_GROUP_ID)
                .setName("测试群组")
                .setCompanyId(INIT_COMPANY_ID)
                .setStatus(GroupStatusEnum.NORMAL)
                .setManagerId("");
        List<GroupMember> members = new ArrayList<>();
        User manager = getUserList().get(0);
        group.setManagerId(manager.getUserId());
        getUserList().forEach(user -> members.add(new GroupMember()
                .setNickName(user.getNickname())
                .setUserId(user.getUserId())
                .setStatus(GroupMemberStatusEnum.NORMAL)
        ));
        group.setGroupMembers(members);
        return group;
    }

    /**
     * 根据用户名获取数据
     *
     * @param username
     * @return
     */
    public static User getUserByName(String username) {
        return generateTestUser().get(username);
    }

    /**
     * 根据用户ID获取用户
     * @param userId
     * @return
     */
    public static User getUserById(String userId)  {
        return getUserList().stream().filter(user -> user.getUserId().equals(userId)).findFirst().orElse(null);
    }

    /**
     * 获取Token
     * @param username
     * @return
     */
    public static String getToken(String username) {
        TokenComponent tokenComponent = new TokenComponent();
        User user = getUserByName(username);
        TokenPayload payload = new TokenPayload()
                .setUserId(user.getUserId())
                .setUsername(user.getUsername())
                .setClientType(ClientTypeEnum.PC);
        return tokenComponent.generateToken(payload);
    }
}
